rm(list = ls())
warnings()
library(fOptions)

f_DollarError<-function(sigma,Option,P){
  #To calculate dollar error of each option
  Call_mkt=unlist(Option[1])
  X=unlist(Option[2])
  S=unlist(P[1])
  risk_free=unlist(P[2])
  dividend=unlist(P[3])
  
  Call_BS=GBSOption(TypeFlag = "c", S = S, X = X, Time =maturity_dt, r = risk_free, b =risk_free, sigma = sigma)
  Dollar_Error=abs(Call_mkt - Call_BS@price) 

  return(Dollar_Error)
}

#Data Input
Call_mkt_1=9.585
Call_mkt_2=3.615
maturity_dt=0.011538462
X1=100
X2=106
S=109.68
risk_free=0.0025
dividend=0.017

Option1=list(Call_mkt_1,X1)
Option2=list(Call_mkt_2,X2)
P=list(S,risk_free,dividend)

sigma_imp1=GBSVolatility(price=Call_mkt_1, TypeFlag="c", S=S, X=X1, Time=maturity_dt,r=risk_free, b=risk_free, tol=0.0001, maxiter=100)
sigma_imp2=GBSVolatility(price=Call_mkt_2, TypeFlag="c", S=S, X=X2, Time=maturity_dt,r=risk_free, b=risk_free, tol=0.0001, maxiter=100)

print(sigma_imp1)
print(sigma_imp2)

####################PLOTING BLACK SCHOLES VALUES
sigma=seq(sigma_imp1, sigma_imp2, by =0.0000001)
v_Call_BS_X1<-vector( mode="numeric", length = length(sigma))
v_Call_BS_X1=GBSOption(TypeFlag = "c", S = S, X = X1, Time =maturity_dt, r = risk_free, b =risk_free, sigma = sigma)@price
v_Call_BS_X1
plot(sigma, v_Call_BS_X1)

####################PlOTTING DOLLAR ERRORS
#sigma=seq(sigma_imp1-0.002, sigma_imp2+0.0000001, by =0.00000001)
#v_DollarError1<-vector( mode="numeric", length = length(sigma))
#v_DollarError1=f_DollarError(sigma,Option1,P)
#v_DollarError2<-vector( mode="numeric", length = length(sigma))
#v_DollarError2=f_DollarError(sigma,Option2,P)
#plot(sigma, v_DollarError1,xlab='sigma', ylab='Dollar Error')
#points(sigma,v_DollarError2,col='red',xlab='sigma', ylab='Dollar Error')
